/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.cred.industries.platform.dao;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cred.industries.platform.bo.CustomerTestBO;
import com.cred.industries.platform.server.IServerResource;
import com.cred.industries.platform.transferObj.SessionResultTrans;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

public class SessionTestDAO {
	private static final Logger logger = LoggerFactory.getLogger(SessionTestDAO.class);

	/**
	 * 
	 * @param createCustBO customer to log in, this customer will be updated with the customer ID and session key
	 * @param server link to the server
	 * @return session key
	 */
	public String Authenticate(CustomerTestBO createCustBO, IServerResource server) {
		
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource resource = server.getResource();
		// baseline OAuth parameters for access to resource
		OAuthParameters params = new OAuthParameters().signatureMethod("HMAC-SHA1").consumerKey(createCustBO.getCreateCustTrans().personaName).version("1.1");

		// OAuth secrets to access resource
		OAuthSecrets secrets = new OAuthSecrets().consumerSecret(createCustBO.getCreateCustTrans().password);

		// if parameters and secrets remain static, filter can be added to each web resource
		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);

		// filter added at the web resource level
		resource.addFilter(filter);
		
		// make the request (signing it in the process)
		String response = resource.path("session").post(String.class);
		
		SessionResultTrans sessionTrans = null;
		try {
			//unmashall the return results
			InputStream inputStream = new ByteArrayInputStream(response.getBytes());
			JAXBContext jaxbContextSession = JAXBContext.newInstance(SessionResultTrans.class);
			Unmarshaller unmarshaller = jaxbContextSession.createUnmarshaller();
			sessionTrans = (SessionResultTrans) unmarshaller.unmarshal(inputStream);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		createCustBO.getCreateCustTrans().customerId = sessionTrans.getCustomerId();
		createCustBO.setSessionId(sessionTrans.getSessionId());		
		return sessionTrans.getSessionId();
	}
	
	public void Logout(CustomerTestBO createCustBO, IServerResource server) {
		
		WebResource resource = server.getResource();
		
		// make the request (signing it in the process)
		String responce = resource.path("session").path(createCustBO.getSessionId())
				.header("Content-Type", javax.ws.rs.core.MediaType.TEXT_PLAIN)
				.header(DAODefines.SESSIONID_HEADER, createCustBO.getSessionId())
				.delete(String.class);
		
		logger.debug("Logout responce " + responce);

	}
	
}
